# -*- coding: utf-8 -*-
# @Time     : 2021/12/1 15:54
# @Author   : wljess
# @File     : 死锁.py
# @Describe : 
# @Software : PyCharm

import time
from threading import Thread, Lock


noodle_lock = Lock()
fork_lock = Lock()


def eat1(name, lock1, lock2):
    '''
    :param name: 姓名
    :param lock1: 面条锁
    :param lock2: 叉子锁
    :return:
    '''
    lock2.acquire()
    print(f"{name}抢到叉子呢...")
    lock1.acquire()
    print(f"{name}抢到面条呢...")
    print(f"{name}开始享用西红柿鸡蛋面条")
    time.sleep(1)
    lock1.release()
    print(f"{name}放下面条...")
    lock2.release()
    print(f"{name}放下叉子...")


def eat2(name, lock1, lock2):
    '''

    :param name: 姓名
    :param lock1: 面条锁
    :param lock2: 叉子锁
    :return:
    '''
    lock1.acquire()
    print(f"{name}抢到面条呢...")
    lock2.acquire()
    print(f"{name}抢到叉子呢...")
    print(f"{name}开始享用酸菜肉丝面条...")
    time.sleep(0.5)
    lock1.release()
    print(f"{name}放下面条...")
    lock2.release()
    print(f"{name}放下叉子...")


if __name__ == '__main__':
    lst_user = ["李白", "黑格尔", "列宁", "希特勒"]
    for inx, user in enumerate(lst_user):
        if inx in (0, 2):
            t = Thread(target=eat2, args=(user, noodle_lock, fork_lock))
        else:
            t = Thread(target=eat1, args=(user, noodle_lock, fork_lock))
        t.start()
